$(function() {
	var end = false;
	$(".widget-content").scroll(function() {
	   if($(".widget-content").scrollTop() + $(".widget-content")[0].clientHeight >= $(".widget-content table")[0].clientHeight) {
	       nextPage();
	   }
	   else
	   {
		   $("#loading").hide();
	   }
	});
	
	$("#from" ).datepicker();
	$("#to" ).datepicker();
	
	todate();
	function todate(){
	    var d=new Date();
		var dat=d.getDate();
		var dd = (dat < 10) ? '0' + dat : dat;
	    var month=d.getMonth()+1;
		var mm = (month < 10) ? '0' + month : month;
		var yyyy = (new Date().getFullYear()).toString();
		$('#from').val(yyyy+"-"+mm+"-"+dd);
	    $('#to').val(yyyy+"-"+mm+"-"+dd);
	}
	
	function nextPage() {
		if(end)
		{
			return;
		}
		pageCount=pageCount+1;
		var fromNum=pageCount*pageNum;
		$("#loading").show();
		$.get(path + "/status/job?id=&date=&fromNum="+fromNum+"&pageNum="+pageNum, function(data) {
			var status = data.status;
			if(status.length == 0) {
				end = true;
				$("#loading").hide();
				return;
			}
			for(var i = 0; i < status.length; i++){
				if(status[i].jobStatus === 'failure'){
					status[i].jobCls = 'error';
				}else{
					status[i].jobCls = '';
				}
			}
			$("#job").append($.renderTmpl("jobStatus", data));
			trs = $('#job tr').each(function(idx, tr){
				this.txt = $(this).text();
			});
			clientFilter();
			$("#loading").hide();
		});
		console.log(pageCount);
	}
	var trs;
	var pageCount=0;
	var pageNum=20;
	render();
	$("#prev").click(function() {
		if(pageCount-1>=0){
			pageCount=pageCount-1;
			var fromNum=pageCount*pageNum;
			$.get(path + "/status/job?id=&date=&fromNum="+fromNum+"&pageNum="+pageNum, function(data) {
				var status = data.status;
				for(var i = 0; i < status.length; i++){
					if(status[i].jobStatus === 'failure'){
						status[i].jobCls = 'error';
					}else{
						status[i].jobCls = '';
					}
				}
				renderAgentStatus(data);
				trs = $('#job tr').each(function(idx, tr){
					this.txt = $(this).text();
				});
				clientFilter();
			});
		}
		console.log(pageCount);
	});
	$("#next").click(function() {
		if(end)
			return;
		
		pageCount=pageCount+1;
		var fromNum=pageCount*pageNum;
		$.get(path + "/status/job?id=&date=&fromNum="+fromNum+"&pageNum="+pageNum, function(data) {
			var status = data.status;
			if(status.length == 0) {
				end = true;
				return;
			}
			for(var i = 0; i < status.length; i++){
				if(status[i].jobStatus === 'failure'){
					status[i].jobCls = 'error';
				}else{
					status[i].jobCls = '';
				}
			}
			renderAgentStatus(data);
			trs = $('#job tr').each(function(idx, tr){
				this.txt = $(this).text();
			});
			clientFilter();
		});
		console.log(pageCount);
	});
	var keyupTimer;
	var jobIdInp = $("#jobId").keyup(function(e) {
		clearTimeout(keyupTimer);
		keyupTimer = setTimeout(function(){
			clientFilter();
		}, 300);
	});
	$(".update-btn").click(function() {
		render();
		//clientFilter();
	});
	function clientFilter() {
		var jobId=getQueryString("id");
		if(jobId=="" || jobId==null){
			jobId = $.trim(jobIdInp.val());
		}else{
			$("#jobId").attr("value",jobId);
		}
		trs.addClass(function(){
			var i = this.txt.indexOf(jobId) < 0;
			if(i){
				return 'hide';
			}else{
				$(this).removeClass('hide');
			}
		});
	}
	
	function getQueryString(name) {
	    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	    var r = window.location.search.substr(1).match(reg);
	    if (r != null) return unescape(r[2]); return null;
	}
	
	function renderAgentStatus(data) {
		$("#job").empty().prepend($.renderTmpl("jobStatus", data));
	}
	function render(){
		var jobId= $("#jobId").attr("value");
		var fileName=$("#fileName").val(); 
		var from=$("#from").val();
		var to=$("#to").val();
		var typeChoose=$("#typeChoose").val();
		$.get(path + "/status/job?jobId="+jobId+"&fromNum=0&pageNum="+pageNum+"" +
				"&fileName="+fileName+"&from="+from+"&to="+to+"&typeChoose="+typeChoose, function(data) {
			var status = data.status;
			for(var i = 0; i < status.length; i++){
				if(status[i].jobStatus === 'failure'){
					status[i].jobCls = 'error';
				}else{
					status[i].jobCls = '';
				}
			}
			renderAgentStatus(data);
			trs = $('#job tr').each(function(idx, tr){
				this.txt = $(this).text();
			});
			clientFilter();
		});
	}
	$("#download").click(function() {
		var jobId= $("#jobId").attr("value");
		var fileName=$("#fileName").val(); 
		var from=$("#from").val();
		var to=$("#to").val();
		var typeChoose=$("#typeChoose").val();
		$("#download").attr("href", path + "/status/downLoadJob?id=" + jobId +"&fromNum=0&pageNum="+pageNum+"" +
				"&fileName="+fileName+"&from="+from+"&to="+to+"&typeChoose="+typeChoose);
	});
});
